package cn.e4j.modular.system.controller;

import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.reqres.response.SuccessResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.modular.system.service.SysRoleModuleService;
import cn.e4j.modular.system.service.SysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 框架入口Controller
 *
 * @author wenchuan.li
 * @date 2018/12/05 16:15
 */
@Controller
@RequestMapping("/index")
public class IndexController extends BaseController {

    @Autowired
    private SysRoleModuleService sysRoleModuleService;

    @Autowired
    private SysUserRoleService sysUserRoleService;

    /**
     * 跳转到登录页面
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    public String index() {
        //CacheUtil.removeAll();
        if (ShiroKit.isAuthenticated() || ShiroKit.getUser() != null) {
            return "/pc/index.html";
        } else {
            return "/pc/login.html";
        }
    }

    @RequestMapping(value = "/getMenus", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData getMenus(@DtoParam DtoWapper dtoWapper) {

        Dto inDto = dtoWapper.getDto();
        ShiroUser user = ShiroKit.getUser();
        String user_id_ = user.getID();
        List<Map<String, String>> menus = null;
        List<Map<String, String>> roleIds = sysUserRoleService.selectRoleidsByUser(user_id_);
        if(roleIds!=null){
            inDto.put("CASCADE_ID","0.003");
            menus = sysRoleModuleService.selectMenusOfRoles(roleIds, inDto);
        }

        return new SuccessResponseData(menus);
    }

}
